package com.common.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;

import com.common.util.Page;


public class PageTag extends TagSupport {

	private static final long serialVersionUID = 1L;
	
	private String pageFunName;
	private String numCount;
	
	public int doEndTag() throws JspException 
	{	
		try {
			JspWriter out = pageContext.getOut();
			HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
			Page page =  (Page) request.getAttribute("page");
			
			if(page!=null && page.getTotalPage()>1){
				if(StringUtils.isBlank(pageFunName)){
					pageFunName = "goPage";
				}
				
				String html = "<div class=\"pagination\"> ";
				
				if(page.getCurPage()>1){
					
					html =html + "<a title=\"首页\" href=\"javascript:void(0)\" onclick=\""+pageFunName+"(1)\" > « 首页 </a>";
					html =html + "<a title=\"上一页\" href=\"javascript:void(0)\" onclick=\""+pageFunName+"("+(page.getCurPage()-1)+")\" >  « 上一页 </a>";
				}else{
					
					html =html + "<a title=\"首页\" href=\"javascript:void(0)\" > « 首页 </a>";
					html =html + "<a title=\"上一页\" href=\"javascript:void(0)\" >  « 上一页 </a>";
				}
				
				int num  = 5;
				if(StringUtils.isNotBlank(numCount) && StringUtils.isNumeric(numCount)){
					num = Integer.valueOf(numCount);
				}
				
				int curPage = page.getCurPage();
				int totalPage = page.getTotalPage();
				String numHtml="";
				if(curPage < totalPage){
					int nestCount =num/2;
					
					if(curPage<=nestCount){
						nestCount = num-curPage;
					}
					
					while(nestCount>0 && curPage<totalPage){
						curPage ++;
						numHtml = numHtml + "<a class=\"number\" title=\""+curPage+"\" href=\"javascript:void(0)\" onclick=\""+pageFunName+"("+curPage+")\" >"+curPage+"</a>";
						nestCount --;
					}
					num = num - ((num/2)-nestCount);
				}
				
				curPage = page.getCurPage();
				while(num>0 && curPage>0){
					if(curPage ==  page.getCurPage()){
						numHtml = "<a class=\"number current\" title=\""+curPage+"\" href=\"javascript:void(0)\" onclick=\""+pageFunName+"("+curPage+")\" >"+curPage+"</a>"+numHtml;
					}else{
						numHtml = "<a class=\"number\" title=\""+curPage+"\" href=\"javascript:void(0)\" onclick=\""+pageFunName+"("+curPage+")\" >"+curPage+"</a>"+numHtml;
					}
					curPage --;
					num --;
				}
				
				html = html + numHtml;
				
				if(page.getCurPage()<page.getTotalPage()){
					
					html =html +"<a title=\"下一页\" href=\"javascript:void(0)\" onclick=\""+pageFunName+"("+(page.getCurPage()+1)+")\" >  « 下一页 </a>";
					html =html + "<a title=\"尾页\" href=\"javascript:void(0)\" onclick=\""+pageFunName+"("+page.getTotalPage()+")\" >  « 尾页 </a>";
				}else{
					
					html =html + "<a title=\"下一页\" href=\"javascript:void(0)\">  « 下一页 </a>";
					html =html +"<a title=\"尾页\" href=\"javascript:void(0)\" >  « 尾页 </a>";
				}
				
				html = html + "</div><div class=\"clear\"></div>";
				out.print(html);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}

	public String getNumCount() {
		return numCount;
	}

	public void setNumCount(String numCount) {
		this.numCount = numCount;
	}

	public int doStartTag() throws JspException 
	{
		return SKIP_BODY;
	}

	public String getPageFunName() {
		return pageFunName;
	}

	public void setPageFunName(String pageFunName) {
		this.pageFunName = pageFunName;
	}
	
}
